package org.example.dialogue.controller;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.ai.result.Result;
import org.example.ai.util.AuthContextHolder;
import org.example.dialogue.form.MessageForm;
import org.example.dialogue.pojo.Conversations;
import org.example.dialogue.vo.MessageVo;
import org.example.dialogue.service.DialogService;
import org.example.login.RedisTokenLookup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zqtao
 */
@Tag(name = "优惠券活动接口管理")
@RestController
@RequestMapping(value="/dialogue")
public class DialogController {


    @Autowired
    private DialogService dialogService;

    @Operation(summary = "调用文心一言AI大模型实现对话")
    @PostMapping("/getMessage")
    @RedisTokenLookup
    public Result<MessageVo> getMessage(@RequestParam Integer initValue, @RequestBody List<MessageForm> messageForms) {
        Long userId = AuthContextHolder.getUserId();
        return Result.ok(dialogService.getMessage(messageForms,userId, initValue).get(0));
    }

    @Operation(summary = "获取对话记录")
    @GetMapping("/getHistoryMessage")
    @RedisTokenLookup
    public Result<List<Conversations>> getHistoryMessage() {
        Long userId = AuthContextHolder.getUserId();
        return Result.ok(dialogService.getHistoryMessage(userId));
    }

    @Operation(summary = "创建历史记录")
    @GetMapping("/initialize")
    @RedisTokenLookup
    public Result<Integer> initializeChat() {
        Long userId = AuthContextHolder.getUserId();
        return Result.ok(dialogService.initializeChat(userId));
    }

    @Operation(summary = "新建对话时，保存对话记录")
    @GetMapping("/saveMessage")
    @RedisTokenLookup
    public Result<String> saveMessage() {
        Long userId = AuthContextHolder.getUserId();
        return Result.ok(dialogService.saveMessage(userId));
    }

    @Operation(summary = "删除对话历史记录")
    @DeleteMapping("/deleteChat/{conversationId}")
    public Result<String> deleteChat(@PathVariable Integer conversationId) {
        return Result.ok(dialogService.deleteChat(conversationId));
    }


}
